package com.guotai.meet.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guotai.meet.entity.Agenda;
import com.guotai.meet.entity.UserSystem;
import com.guotai.meet.service.AgendaService;
import com.guotai.meet.service.UserSystemService;
import com.guotai.meet.utils.DateUtil;

@Controller
@RequestMapping("/userSystem")
public class UserSystemController {
	
	private  int pageSize = 10;
	
	private  int pageIndex = 0;
	
	@Autowired
    private UserSystemService userService;
	
	@Autowired
	private AgendaService agendaService;
	
	/**
	 * 首页检查是否登录
	 * @param request
	 * @return
	 * @author yqy
	 */
    @RequestMapping("/index")
    public String login(HttpServletRequest request) {
    	UserSystem user=(UserSystem)request.getSession().getAttribute("loginUser");
//    	UserSystem users = new UserSystem();
//    	String index = request.getParameter("pageIdex");
//    	String size = request.getParameter("pageSize");
//    	if((!StringUtils.isEmpty(index))&&(!StringUtils.isEmpty(size))) {
//    		pageIndex = Integer.parseInt(index)*pageSize;
//    	}
//    	
//    	users.setPageIndex(pageIndex);
//    	users.setPageSize(pageSize);
    	
    	request.setAttribute("users", user);
    	if(StringUtils.isEmpty(user)){
    		return "redirect:/userSystem/loginIndex";
    	}else{
    		//超级管理员
    		if(1==user.getIsAdmin()) {
    		//	List<UserSystem>  listUser = userService.queryUserList(users);
    		//	request.setAttribute("listUser", listUser);
    			
    			List<Agenda> agendaList = agendaService.queryAgendaList(null);
    			
    			if(null!=agendaList && agendaList.size()>0) {
    				request.setAttribute("agenda", agendaList.get(0));
    			}else {
    				request.setAttribute("agenda", new Agenda());
    			}
    			
    			return "/homePage";
    		}else {
//    			users.setParentId(user.getUserId());
//    			users.setCompany(user.getCompany());
//    			List<UserSystem>  listUser = userService.queryUserList(users);
//    			request.setAttribute("listUser", listUser);
    			if(2 == user.getIsAdmin()) {
    				List<Agenda> agendaList = agendaService.queryAgendaList(null);
    				if(null!=agendaList && agendaList.size()>0) {
        				request.setAttribute("agenda", agendaList.get(0));
        			}else {
        				request.setAttribute("agenda", new Agenda());
        			}
    				return "/homePage";
    			}else {
    				return "redirect:/agenda/homePage";
    			}
    		}
    	}
    }
    
    /**
     * 跳转到详细页
     * @param request
     * @return
     */
    @RequestMapping("/details")
    public String setting(HttpServletRequest request) {
    	UserSystem user=(UserSystem)request.getSession().getAttribute("loginUser");
		List<Agenda> agendaList = agendaService.queryAgendaList(null);
		
		if(null!=agendaList && agendaList.size()>0){
			request.setAttribute("agenda", agendaList.get(0));
		}else {
			request.setAttribute("agenda", new Agenda());
		}
		request.setAttribute("agendaList", agendaList);
		request.setAttribute("users", user);
    	return "/details";
    }
    
    /**
     * 跳转到登录页面
     * @param request
     * @return
     * @author yqy
     */
    @RequestMapping("/loginIndex")
    public String loginIndex(HttpServletRequest request) {
    	return "/login";
    }
    
    /**
     * 退出登录
     * @param request
     * @return
     * @author yqy
     */
    @RequestMapping("/logOut")
    public String logOut(HttpServletRequest request,HttpSession httpSession) {
    	UserSystem user=(UserSystem)request.getSession().getAttribute("loginUser");
    	if(null != user) {
			httpSession.removeAttribute("loginUser");
		}
    	return "redirect:/userSystem/loginIndex";
    }
    
    
    /**
     * 登录验证
     * @param request
     * @return
     * @author yqy
     */
    @RequestMapping("/login")
    @ResponseBody
    public Object loginVerification(HttpServletRequest request) {
    	Object result="0";
		String userName=request.getParameter("userName");
		String passWord=request.getParameter("password");
		
		UserSystem user=userService.selectByUserName(userName);
		if(user!=null&&userName.equals(user.getUserName())&&user.getPassword().equals(passWord)){
			//user.setPassword(null);
			result="1";
			request.getSession().setAttribute("loginUser", user);
		}
        return result;
    }
    
    
    /**
     * 删除
     * @param request
     * @return
     * @author yqy
     */
    @SuppressWarnings("finally")
	@RequestMapping("/deleteUser")
    @ResponseBody
    public Object deleteUser(HttpServletRequest request) {
    	Object result="0";
		String userId=request.getParameter("userId");
		try {
			if(!StringUtils.isEmpty(userId)) {
				userService.deleteByPrimaryKey(userId);
				result="1";
			} 
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			return result;
		}
    }
    
    
    /**
     * 模糊搜索
     * @param request
     * @return
     * @author Information
     */
    @RequestMapping("/searchUser")
    @ResponseBody
    public List<UserSystem> searchUser(HttpServletRequest request) {
    	UserSystem user=(UserSystem)request.getSession().getAttribute("loginUser");
    	String search = ("".equals(request.getParameter("search"))|| (request.getParameter("search")==null))?null:request.getParameter("search");
    	String index = request.getParameter("pageIdex");
    	
    	if((!StringUtils.isEmpty(index))&&(!"1".equals(index))) {
    		pageIndex = (Integer.parseInt(index)-1)*pageSize;
    	}else {
    		pageIndex = 0;
    	}
    	
    	String company = null;
    	//2表示供应商管理员
    	if((!StringUtils.isEmpty(user))&& user.getIsAdmin()==2) {
    		company = user.getCompany();
    	}
    	
    	List<UserSystem>  listUser = userService.findUserList(search, company, pageIndex, pageSize);
    	
    	return listUser;
    		
    }
    
    /**
     * 跳转到编辑页面
     * @param request
     * @return
     * @author yqy
     */
    @RequestMapping("/editPage")
    public String editPage(HttpServletRequest request) {
    	UserSystem loginUser=(UserSystem)request.getSession().getAttribute("loginUser");
    	String userId = request.getParameter("userId");
    	UserSystem user=userService.selectByPrimaryKey(userId);
    	String state = StringUtils.isEmpty(user)?"1":"0";
    	request.setAttribute("user", user);
    	request.setAttribute("state", state);
    	request.setAttribute("loginUser", loginUser);
    	//超级管理员才有权限 
//    	if(1==loginUser.getIsAdmin()) {
//    		return "/editUserPage";
//    	}else {
//    		return "/editPage";
//    	}
    	return "/editPage";
    }
    /**
     * 跳转到编辑页面
     * @param request
     * @return
     * @author yqy
     */
    @RequestMapping("/addUserPage")
    public String addUserPage(HttpServletRequest request) {
    	UserSystem loginUser=(UserSystem)request.getSession().getAttribute("loginUser");
//    	String userId = request.getParameter("userId");
    	request.setAttribute("loginUser", loginUser);
    	//超级管理员才有权限 
//    	if(1==loginUser.getIsAdmin()) {
//    		return "/editUserPage";
//    	}else {
//    		return "/editPage";
//    	}
    	return "/addUserPage";
    }
    
    /**
     * 修改或新增用户
     * @param request
     * @param user
     * @author Information
     * @return
     */
	@RequestMapping("/inUpUserData")
    @ResponseBody
    public String inUpUserData(HttpServletRequest request,UserSystem user) {
		//把电话号码设置为用户名
		user.setUserName(user.getPhone());
		UserSystem loginUser=(UserSystem)request.getSession().getAttribute("loginUser");
    	try {
    		String dateTime = DateUtil.formatTimesTampDate(new Date());
    		
    		List<UserSystem>  listUser = userService.queryUserList(new UserSystem(user.getUserName()));
    	
    		if(StringUtils.isEmpty(user.getUserId()) || "".equals(user.getUserId())) {
    			if(listUser.size()>0) {
        			return "2";
        		}
    			String uid = UUID.randomUUID().toString();
    			user.setUserId(uid);
    			user.setCreateTime(dateTime);
    			userService.insertSelective(user);
    			return "1";
    		}else {
    			if(listUser.size()>1) {
        			return "2";
        		}
    			user.setUpdateTime(dateTime);
    			user.setUpdateUser(loginUser.getRealName());
    			userService.updateByPrimaryKeySelective(user);
    			return "1";
    		}
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
    }
}
